using System.Collections.Generic;
using UnityEngine;

namespace Function.SkillEditor.Runtime
{
    [ExecuteInEditMode]
    public class ShapeDrawerManager : MonoBehaviour
    {
        public Color color = Color.white;
        private Material lineMat;
        private List<SkillTriggerClip> _skillAttackClip = new List<SkillTriggerClip>();
        private Transform _parent;

        void Start()
        {
            color = new Color(0f / 255f, 174f / 255f, 38f / 255f, 255f / 255f);
            lineMat = new Material(Shader.Find("SCore/PureColor"));
            lineMat.SetColor("_Color", color);
        }

        public void AddClip(GameObject gameObject, SkillTriggerClip skillTriggerClip)
        {
            _parent = gameObject.transform;
            if (!_skillAttackClip.Contains(skillTriggerClip))
            {
                _skillAttackClip.Add(skillTriggerClip);
            }
        }

        public void RemoveClip(SkillTriggerClip skillTriggerClip)
        {
            if (_skillAttackClip.Contains(skillTriggerClip))
            {
                _skillAttackClip.Remove(skillTriggerClip);
            }
        }

        void OnRenderObject()
        {
            if (_parent == null)
            {
                _skillAttackClip.Clear();
                return;
            }

            lineMat.SetColor("_Color", color);
            for (int offset = _skillAttackClip.Count - 1; offset >= 0; offset--)
            {
                DrawAttackClip(_skillAttackClip[offset]);
            }
        }

        void DrawAttackClip(SkillTriggerClip skillTriggerClip)
        {
            if (skillTriggerClip._skillAttackShape == SkillAttackShape.None)
            {
                return;
            }

            if (skillTriggerClip._skillAttackShape == SkillAttackShape.Box)
            {
                var center = skillTriggerClip._pos.ToVector3();
                var size = new Vector3(skillTriggerClip._param1.AsFloat(), skillTriggerClip._param2.AsFloat(),
                    skillTriggerClip._param3.AsFloat());
                Vector3[] points = new Vector3[8]
                {
                    new Vector3(-size.x / 2.0f, -size.y / 2.0f, -size.z / 2.0f),
                    new Vector3(size.x / 2.0f, -size.y / 2.0f, -size.z / 2.0f),
                    new Vector3(size.x / 2.0f, -size.y / 2.0f, size.z / 2.0f),
                    new Vector3(-size.x / 2.0f, -size.y / 2.0f, size.z / 2.0f),

                    new Vector3(-size.x / 2.0f, size.y / 2.0f, -size.z / 2.0f),
                    new Vector3(size.x / 2.0f, size.y / 2.0f, -size.z / 2.0f),
                    new Vector3(size.x / 2.0f, size.y / 2.0f, size.z / 2.0f),
                    new Vector3(-size.x / 2.0f, size.y / 2.0f, size.z / 2.0f)
                };
                for (int i = 0; i < points.Length; ++i)
                {
                    points[i] += center;
                    points[i] = _parent.TransformPoint(points[i]);
                }

                DrawLine(points[0], points[1]);
                DrawLine(points[0], points[3]);
                DrawLine(points[0], points[4]);

                DrawLine(points[5], points[1]);
                DrawLine(points[5], points[4]);
                DrawLine(points[5], points[6]);

                DrawLine(points[2], points[1]);
                DrawLine(points[2], points[3]);
                DrawLine(points[2], points[6]);

                DrawLine(points[7], points[3]);
                DrawLine(points[7], points[4]);
                DrawLine(points[7], points[6]);
            }
        }

        void DrawLine(Vector3 pointA, Vector3 pointB)
        {
            if (lineMat == null)
                return;

            GL.Begin(GL.LINES);
            lineMat.SetPass(0);
            GL.Color(new Color(color.r, color.g, color.b, color.a));
            GL.Vertex3(pointA.x, pointA.y, pointA.z);
            GL.Vertex3(pointB.x, pointB.y, pointB.z);
            GL.End();
        }
    }
}